const feathers = require('@feathersjs/feathers');
const errors = require('@feathersjs/errors');
const path = require('path');
const fs = require('fs');

const Mod = {
  setMchCert(ctx){
    if(!ctx.data) return Promise.reject(new errors.BadRequest('empty data'));
    //先读取默认配置项
    try{
      let conf = fs.readFileSync(path.join(__dirname,'../../../config/docker.json'));
      let o = JSON.parse(conf.toString('utf8'));
      //拿到商户号
      if(!o.mchId) return Promise.reject(new errors.BadRequest('get MCH_ID failed'));
      if(!fs.existsSync(path.join(__dirname,`../../../config/${o.mchId}`))){
        fs.mkdirSync(path.join(__dirname,`../../../config/${o.mchId}`));
      }
      fs.writeFileSync(path.join(__dirname,`../../../config/${o.mchId}/apiclient_key.pem`), ctx.data.key);
      fs.writeFileSync(path.join(__dirname,`../../../config/${o.mchId}/apiclient_cert.pem`), ctx.data.cert);
      ctx.result = {
        code: 1,
        message: 'success'
      };
    }catch(e){
      return Promise.reject(new errors.BadRequest(e));
    }
    return feathers.SKIP;
  }
};

module.exports = {
  before: {
    all: [],
    find: [ctx=>{
      try{
        let conf = fs.readFileSync(path.join(__dirname,'../../../config/docker.json'));
        let o = JSON.parse(conf.toString('utf8'));
        ctx.result = {
          code: 1,
          message: 'success',
          data: o
        };
      }catch(e){
        return Promise.reject(new errors.BadRequest(e));
      }
      return feathers.SKIP;
    }],
    get: [],
    create: [ctx=>{
      if(!ctx.data) return Promise.reject(new errors.BadRequest('empty data'));
      //先读取默认配置项
      try{
        let conf = fs.readFileSync(path.join(__dirname,'../../../config/default.json'));
        let o = JSON.parse(conf.toString('utf8'));
        //把默认配置和前端传过来的自定义配置合并
        let obj = Object.assign(o,ctx.data);
        //保存成文件
        fs.writeFileSync(path.join(__dirname,'../../../config/docker.json'),JSON.stringify(obj));
        ctx.result = {
          code: 1,
          message: 'success'
        };
      }catch(e){
        return Promise.reject(new errors.BadRequest(e));
      }
      return feathers.SKIP;
    }],
    update: [ctx=>{
      try{
        let conf = fs.readFileSync(path.join(__dirname,'../../../config/docker.json'));
        let obj = JSON.parse(conf.toString('utf8'));
        for(let i in ctx.data){
          obj[i] = ctx.data[i];
        }
        fs.writeFileSync(path.join(__dirname,'../../../config/docker.json'),JSON.stringify(obj));
        ctx.result = {
          code: 1,
          message: 'success'
        };
      }catch(e){
        return Promise.reject(new errors.BadRequest(e));
      }
      return feathers.SKIP;
    }],
    patch: [ctx=>{
      if(Mod[ctx.id]){
        return Mod[ctx.id](ctx);
      }
    }],
    remove: []
  },

  after: {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  },

  error: {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  }
};
